package com.drug.mingkang.controller;

import com.alibaba.druid.util.StringUtils;
import com.drug.mingkang.model.HqDrugmaterials;
import com.drug.mingkang.service.HqDrugmaterialsService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 药品的材料(HqDrugmaterials)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:12
 */
@RestController
@RequestMapping("hqDrugmaterials")
public class HqDrugmaterialsController {
    /**
     * 服务对象
     */
    @Resource
    private HqDrugmaterialsService hqDrugmaterialsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public HqDrugmaterials selectOne(String id) {
        return this.hqDrugmaterialsService.queryById(id);
    }

    /**
     * 原材料,药品和药品材料关联查询指定行数据
     * @param page
     * @param limit
     * @param drugId
     * @return
     */
    @RequestMapping("selectAll")
    public Map<String,Object> selectAll(int page,int limit,String drugId){
        System.out.println(drugId+"-------------");
        List<HqDrugmaterials> list = this.hqDrugmaterialsService.selectByMany(page, limit,drugId);
        int count = this.hqDrugmaterialsService.selectCount();
        Tools tools = new Tools();
        return tools.limit(list,count);
    }

    /**
     * 增加药品材料
     * @param list
     * @param drugId
     * @return
     */
    @RequestMapping("addOrUpdate")
    public ResponseMessage addOrUpdate(String list,String drugId){
        Gson gson = new Gson();
        List<HqDrugmaterials> hqDrugmaterials = gson.fromJson(list, new TypeToken<List<HqDrugmaterials>>(){}.getType());
        for (HqDrugmaterials materials:hqDrugmaterials) {
            materials.setDrugId(drugId);
        }
        int i = this.hqDrugmaterialsService.insertBatch(hqDrugmaterials);
        ResponseMessage responseMessage = new ResponseMessage();
        if (i<=0){
            responseMessage.setStatus(0);
            responseMessage.setMessage("操作失败");
        }else{
            responseMessage.setStatus(1);
            responseMessage.setMessage("操作成功");
        }
        return responseMessage;
    }
}